home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 24 aspnet applications / aspnetsecurity / loginpage.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-18  |  4.6 KB  |  107 lines

  1. Imports System.Web.Security
  2. Imports System.Data.OleDb
  3.  
  4. Public Class LoginPage
  5.     Inherits System.Web.UI.Page
  6.     Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox
  7.     Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
  8.     Protected WithEvents chkRemember As System.Web.UI.WebControls.CheckBox
  9.     Protected WithEvents btnLogin As System.Web.UI.WebControls.Button
  10.     Protected WithEvents Form3 As System.Web.UI.HtmlControls.HtmlForm
  11.     Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
  12.     Protected WithEvents hprNewUser As System.Web.UI.WebControls.HyperLink
  13.  
  14. #Region " Web Form Designer Generated Code "
  15.  
  16.     'This call is required by the Web Form Designer.
  17.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  18.  
  19.     End Sub
  20.  
  21.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  22.         'CODEGEN: This method call is required by the Web Form Designer
  23.         'Do not modify it using the code editor.
  24.         InitializeComponent()
  25.     End Sub
  26.  
  27. #End Region
  28.  
  29.     ' this constants decides whether the user name is validated the
  30.     ' entries in web.config or by using a custom validation mechanism
  31.     ' that relies on some other technique (eg a database of users)
  32. #Const USE_CUSTOM_COOKIES = True
  33.  
  34.     ' the user is attempting to log in
  35.  
  36.     Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
  37.         ' you can replace the following line with a call to the 
  38.         ' AuthenticateUser routine for custom authentication
  39.  
  40.         If FormsAuthentication.Authenticate(txtUsername.Text, txtPassword.Text) Then
  41.             ' the user name has been found in web.config
  42.  
  43. #If USE_CUSTOM_COOKIES = False Then
  44.             ' use transient or permanent default cookies
  45.             FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, chkRemember.Checked)
  46. #Else
  47.             ' use custom cookies that, if the user decides to remember
  48.             ' her password, expire after one week
  49.             RedirectFromLoginPageEx(txtUsername.Text, chkRemember.Checked, 7)
  50. #End If
  51.  
  52.         Else
  53.             ' the user couldn't be authenticated
  54.             lblMessage.Text = "Invalid user name or password"
  55.         End If
  56.     End Sub
  57.  
  58.  
  59.     ' this procedure checks the user name against a database of users
  60.     ' (it is provided as an example, as no user database is provided
  61.     '  with this demo)
  62.  
  63.     ' replace this with a real connection string
  64.     Dim PasswordDBConnString As String = "... a connection string ..."
  65.  
  66.     Function AuthenticateUser(ByVal username As String, ByVal password As String) As Boolean
  67.         ' Open the connection to the database holding user names and passwords.
  68.         Dim cn As New OleDbConnection(PasswordDBConnString)
  69.         cn.Open()
  70.         ' Read the record for this user.
  71.         Dim cmd As New OleDbCommand("SELECT * FROM Users WHERE UserName=?", cn)
  72.         cmd.Parameters.Add("username", username)
  73.         Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
  74.  
  75.         If dr.Read AndAlso dr("Password") = password Then
  76.             ' Authenticate user if there is a record and the password is correct.
  77.             AuthenticateUser = True
  78.         End If
  79.         ' Close the DataReader (and the connection).
  80.         dr.Close()
  81.     End Function
  82.  
  83.     ' A custom routine that works like FormsAuthentication.RedirectFromLoginPage
  84.     ' but lets you control the authentication cookie's expiration date.
  85.  
  86.     Function RedirectFromLoginPageEx(ByVal username As String, ByVal persistentCookie As Boolean, Optional ByVal expirationDays As Integer = -1) As Boolean
  87.         ' Get the URL of the requested resource.
  88.         Dim url As String = FormsAuthentication.GetRedirectUrl(username, persistentCookie)
  89.         ' Create the authentication cookie.
  90.         ' (The cookie path can be omitted, because it defaults to "/", the entire site.)
  91.         FormsAuthentication.SetAuthCookie(username, persistentCookie, "/")
  92.  
  93.         If persistentCookie And expirationDays > 0 Then
  94.             ' Get a reference to the cookie just created.
  95.             Dim cookie As HttpCookie = Response.Cookies(FormsAuthentication.FormsCookieName)
  96.             ' Set its expiration date.
  97.             cookie.Expires = Now.AddDays(expirationDays)
  98.             ' Uncomment next line to ensure the cookie can travel only over https.
  99.             'cookie.Secure = True
  100.         End If
  101.  
  102.         ' Redirect to the resource that was requested originally.
  103.         Response.Redirect(url)
  104.     End Function
  105.  
  106. End Class
  107.